package com.abso.sunlight.explorer.handlers;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;

import com.abso.sunlight.explorer.LegislatorSearch;
import com.abso.sunlight.explorer.SelectionUtils;
import com.abso.sunlight.explorer.dialogs.LegislatorSearchDialog;
import com.abso.sunlight.explorer.jobs.RunLegislatorSearchJob;
import com.abso.sunlight.explorer.viewers.ExplorerTreeNode;

/**
 * Configures the selected search.
 */
public class EditLegislatorSearchHandler extends AbstractHandler {

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
     */
    public Object execute(ExecutionEvent event) throws ExecutionException {
        Shell shell = HandlerUtil.getActiveShell(event);
        IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
        ISelection sel = HandlerUtil.getCurrentSelection(event);
        ExplorerTreeNode node = SelectionUtils.getSelectedExplorerTreeNode(sel, true);
        if (node != null) {
            LegislatorSearch search = (LegislatorSearch) node.getData();
            LegislatorSearchDialog dialog = new LegislatorSearchDialog(shell, search);
            if (dialog.open() == Window.OK) { // Run
                new RunLegislatorSearchJob(window, search).schedule();
            }
        }
        return null;
    }

}
